﻿using SpyApp.Models;
using SpyApp.Models.BaseCommon.Enum;
using SpyApp.Models.BaseCommon.ERROR_CODE;
using SpyApp.Models.EntytiRepossitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SpyApp.Controllers
{
    public class LocationController : Controller
    {
        string _messageError = "";
        public UserRepository userRepository { get; set; }

        public LocationController()
        {
            this.userRepository = new UserRepository();
        }

        public ActionResult Index(string deviceid, int page = 1)
        {
            string username = HttpContext.User.Identity.Name;
            Device device = new Device();
            // tìm thiết bị            
            ERROR_CODE_ITEMS_SELECT errCode = userRepository.GetDeviceByUserName(deviceid, username, ref _messageError, ref device);
            if (errCode != ERROR_CODE_ITEMS_SELECT.SUCCESS)
            {
                // none device => first device
                IEnumerable<Device> listDevice = new List<Device>();
                errCode = userRepository.GetListDeviceByUserName(username, ref _messageError, ref listDevice);
                if (errCode == ERROR_CODE_ITEMS_SELECT.SUCCESS && listDevice.Count() > 0)
                {
                    device = listDevice.First();
                }
                else
                {
                    // non device => message
                    return RedirectToAction("NoDevice", "Device");
                }
            }
            //NGhĩa: lấy danh sách tin nhắn
            List<Location> listLocation = device.LocationTracking;
            CustomPageList<Location> pagelist = new CustomPageList<Location>(listLocation, page, StaticValue.DEFAUT_PAGESIZE);

            return View(pagelist);
        }

        

    }
}
